package com.example.lambda.stream;

import com.example.lambda.test4.Employee;
import lombok.AllArgsConstructor;
import lombok.Data;
import lombok.NoArgsConstructor;
import org.junit.Test;

import java.util.Arrays;
import java.util.List;
import java.util.Map;
import java.util.function.Function;
import java.util.stream.Collectors;

/**
 * @author readpage
 * @create 2022-11-02 15:20
 */
public class GroupTest {
    @Test
    public void test() {
        List<Employee> employeeList= Arrays.asList(new Employee(101,"张三",18,9999.99), new Employee(102,"李四",59,6666.66),new Employee(103,"王五",28,3333.33), new Employee(104,"赵六",8,7777.77));
        //id作为Map的key,name作为value的集合
        Map<Integer, String> collect1= employeeList.stream().collect(Collectors.toMap(Employee::getId, Employee::getName));
        System.out.println(collect1); //{101=张三, 102=李四, 103=王五, 104=赵六}/**
        //id作为map的集合，Employee对象作为Map的value
        Map<Integer, Employee> collect2= employeeList.stream().collect(Collectors.toMap(Employee::getId, t-> t));
        System.out.println(collect2); //{101=Employee(id=101, name=张三, age=18, salary=9999.99), 102=Employee(id=102, name=李四, age=59, salary=6666.66), 103=Employee(id=103, name=王五, age=28, salary=3333.33), 104=Employee(id=104, name=赵六, age=8, salary=7777.77)}/**
        //id作为map的集合，Employee对象作为Map的value
        Map<Integer, Employee> collect3= employeeList.stream().collect(Collectors.toMap(Employee::getId, Function.identity()));
        System.out.println(collect3); //{101=Employee(id=101, name=张三, age=18, salary=9999.99), 102=Employee(id=102, name=李四, age=59, salary=6666.66), 103=Employee(id=103, name=王五, age=28, salary=3333.33), 104=Employee(id=104, name=赵六, age=8, salary=7777.77)}}
         // id作为Map的key,Employee作为value的集合。 如果ID相等，则返回第一个id的Employee
        Map<Integer, Employee> collect4= employeeList.stream().collect(Collectors.toMap(Employee::getId, t-> t,(n1, n2)-> n1));
        System.out.println(collect2);
    }

    @Data
    @AllArgsConstructor
    @NoArgsConstructor
    static class Banana {
        private Integer id;
        private String name;
        private Integer weight;
        private Integer price;


        public Banana plus(Banana banana) {
            this.setPrice(this.getPrice() + banana.getPrice());
            this.setWeight(this.getWeight() + banana.getWeight());
            return this;
        }
    }

    private static List<Banana> bananas = Arrays.asList(
            new Banana(1, "banana1", 10, 20),
            new Banana(2, "banana2", 20, 10),
            new Banana(3, "banana3", 10, 20),
            new Banana(4, "banana1", 30, 30),
            new Banana(5, "banana2", 10, 40),
            new Banana(6, "banana1", 40, 30),
            new Banana(7, "banana3", 50, 20),
            new Banana(8, "banana2", 10, 50)
    );
    // 分组合并求和
    @Test
    public void test2() {

        Map<String, Banana> map = bananas.stream().collect(Collectors.toMap(
                Banana::getName,
                t -> t,
                (b1, b2) -> b1.plus(b2)
        ));
        System.out.println(map);
        List<Banana> collect = map.values().stream().collect(Collectors.toList());
        System.out.println(collect);

        // 计算相同key数量
        bananas.stream().collect(Collectors.toMap(
                Banana::getName,
                t -> 1,
                Integer::sum
        )).entrySet().stream().collect(Collectors.toList()).forEach(System.out::println);

    }

    // 分组数据不合并
    @Test
    public void test3() {
        Map<String, List<Banana>> map = bananas.stream().collect(Collectors.groupingBy(t -> t.getName()));
        System.out.println(map);

        Map<String, Map<String, List<Banana>>> map2 = bananas.stream().collect(Collectors.groupingBy(t -> {
            if ((t.getId() & 1) == 1) {
                return "奇数";
            } else {
                return "偶数";
            }
        }, Collectors.groupingBy(Banana::getName)));

        System.out.println(map2);
    }
}
